package cn.itcast.producer;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.KafkaSerializationSchema;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.annotation.Nullable;
import java.util.Properties;

/**
 * @author lilulu
 * @date 2023-04-16 19:27
 */
public class FlinkKafkaWriter {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(3);
        Properties props = new Properties();
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "node1:9092,node2:9092,node3:9092");
        props.setProperty(ProducerConfig.ACKS_CONFIG, "0");
        FlinkKafkaProducer flinkKafkaProducer = new FlinkKafkaProducer(
                "vehicle_data",
                new KafkaSerializationSchema<String>() {
                    @Override
                    public ProducerRecord<byte[], byte[]> serialize(String element, @Nullable Long aLong) {
                        return new ProducerRecord<>("vehicle_data", element.getBytes());
                    }
                },
                props,
                FlinkKafkaProducer.Semantic.EXACTLY_ONCE

        );
        DataStreamSource<String> dataStreamSource = env.readTextFile("E:\\vehicleProject\\CarNetworkingSystem\\data\\sourcedata.txt");
        dataStreamSource.addSink(flinkKafkaProducer);
        env.execute();
    }
}
